{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import cv2\n",
    "import numpy as np\n",
    "\n",
    "# Compute low order moments(1,2,3)\n",
    "def color_moments(filename):\n",
    "    img = cv2.imread(filename)\n",
    "    if img is None:\n",
    "        return\n",
    "    # Convert BGR to HSV colorspace\n",
    "    hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)\n",
    "    # Split the channels - h,s,v\n",
    "    h, s, v = cv2.split(hsv)\n",
    "    # Initialize the color feature\n",
    "    color_feature = []\n",
    "    # N = h.shape[0] * h.shape[1]\n",
    "    # The first central moment - average \n",
    "    h_mean = np.mean(h)  # np.sum(h)/float(N)\n",
    "    s_mean = np.mean(s)  # np.sum(s)/float(N)\n",
    "    v_mean = np.mean(v)  # np.sum(v)/float(N)\n",
    "    color_feature.extend([h_mean, s_mean, v_mean])\n",
    "    # The second central moment - standard deviation\n",
    "    h_std = np.std(h)  # np.sqrt(np.mean(abs(h - h.mean())**2))\n",
    "    s_std = np.std(s)  # np.sqrt(np.mean(abs(s - s.mean())**2))\n",
    "    v_std = np.std(v)  # np.sqrt(np.mean(abs(v - v.mean())**2))\n",
    "    color_feature.extend([h_std, s_std, v_std])\n",
    "    # The third central moment - the third root of the skewness\n",
    "    h_skewness = np.mean(abs(h - h.mean())**3)\n",
    "    s_skewness = np.mean(abs(s - s.mean())**3)\n",
    "    v_skewness = np.mean(abs(v - v.mean())**3)\n",
    "    h_thirdMoment = h_skewness**(1./3)\n",
    "    s_thirdMoment = s_skewness**(1./3)\n",
    "    v_thirdMoment = v_skewness**(1./3)\n",
    "    color_feature.extend([h_thirdMoment, s_thirdMoment, v_thirdMoment])\n",
    "\n",
    "    return color_feature\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "color_moments('C:\\Users\\zhuwenjing\\Desktop\\test_cut.jpg')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "save() takes at least 2 arguments (1 given)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-6-0e4659084385>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m      8\u001b[0m \u001b[0mbox\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m100\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m100\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m100\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m100\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      9\u001b[0m \u001b[0mroi\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mimg\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mcrop\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mbox\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 10\u001b[1;33m \u001b[0mroi\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msave\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     11\u001b[0m \u001b[0mplt\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msubplot\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m2\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m2\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mplt\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mtitle\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'roi'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     12\u001b[0m \u001b[0mplt\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mimshow\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mroi\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mplt\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0maxis\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'off'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mTypeError\u001b[0m: save() takes at least 2 arguments (1 given)"
     ]
    }
   ],
   "source": [
    "from PIL import Image\n",
    "import matplotlib.pyplot as plt\n",
    "img=Image.open('test.jpg')  #打开图像\n",
    "plt.figure(\"beauty\")\n",
    "plt.subplot(1,2,1), plt.title('origin')\n",
    "plt.imshow(img),plt.axis('off')\n",
    "\n",
    "box=(100,100,100,100)\n",
    "roi=img.crop(box)\n",
    "roi.save()\n",
    "plt.subplot(1,2,2), plt.title('roi')\n",
    "plt.imshow(roi),plt.axis('off')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "璇疯緭鍏ュ浘鐗囪緭鍑虹洰褰曪紙涓嶈緭鍏ヨ矾寰勫垯琛ㄧず浣跨敤婧愬浘鐗囨墍鍦ㄧ洰褰曪級锛�tes.jpg\n"
     ]
    },
    {
     "ename": "NameError",
     "evalue": "name 'tes' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-12-539b70aa3c96>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m     32\u001b[0m \u001b[0msrc\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;34m'test.jpg'\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     33\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0mos\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mpath\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0misfile\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0msrc\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 34\u001b[1;33m     \u001b[0mdstpath\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0minput\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'请输入图片输出目录（不输入路径则表示使用源图片所在目录）：'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     35\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     36\u001b[0m     \u001b[1;32mif\u001b[0m \u001b[1;33m(\u001b[0m\u001b[0mdstpath\u001b[0m \u001b[1;33m==\u001b[0m \u001b[1;34m''\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;32mor\u001b[0m \u001b[0mos\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mpath\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mexists\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mdstpath\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mD:\\Anaconda2\\lib\\site-packages\\ipykernel\\ipkernel.pyc\u001b[0m in \u001b[0;36m<lambda>\u001b[1;34m(prompt)\u001b[0m\n\u001b[0;32m    162\u001b[0m             \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_sys_eval_input\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mbuiltin_mod\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0minput\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    163\u001b[0m             \u001b[0mbuiltin_mod\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mraw_input\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mraw_input\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 164\u001b[1;33m             \u001b[0mbuiltin_mod\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0minput\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;32mlambda\u001b[0m \u001b[0mprompt\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;34m''\u001b[0m\u001b[1;33m:\u001b[0m \u001b[0meval\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mraw_input\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mprompt\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    165\u001b[0m         \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_save_getpass\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mgetpass\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mgetpass\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    166\u001b[0m         \u001b[0mgetpass\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mgetpass\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mgetpass\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mD:\\Anaconda2\\lib\\site-packages\\ipykernel\\ipkernel.pyc\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n",
      "\u001b[1;31mNameError\u001b[0m: name 'tes' is not defined"
     ]
    }
   ],
   "source": [
    "import os\n",
    "from PIL import Image\n",
    "\n",
    "def splitimage(src, rownum, colnum, dstpath):\n",
    "    img = Image.open(src)\n",
    "    w, h = img.size\n",
    "    if rownum <= h and colnum <= w:\n",
    "        print('Original image info: %sx%s, %s, %s' % (w, h, img.format, img.mode))\n",
    "        print('开始处理图片切割, 请稍候...')\n",
    "\n",
    "        s = os.path.split(src)\n",
    "        if dstpath == '':\n",
    "            dstpath = s[0]\n",
    "        fn = s[1].split('.')\n",
    "        basename = fn[0]\n",
    "        ext = fn[-1]\n",
    "\n",
    "        num = 0\n",
    "        rowheight = h // rownum\n",
    "        colwidth = w // colnum\n",
    "        for r in range(rownum):\n",
    "            for c in range(colnum):\n",
    "                box = (c * colwidth, r * rowheight, (c + 1) * colwidth, (r + 1) * rowheight)\n",
    "                img.crop(box).save(os.path.join(dstpath, basename + '_' + str(num) + '.' + ext), ext)\n",
    "                num = num + 1\n",
    "\n",
    "        print('图片切割完毕，共生成 %s 张小图片。' % num)\n",
    "    else:\n",
    "        print('不合法的行列切割参数！')\n",
    "\n",
    "# src = input('请输入图片文件路径：')\n",
    "src = 'test.jpg'\n",
    "if os.path.isfile(src):\n",
    "    dstpath = input('请输入图片输出目录（不输入路径则表示使用源图片所在目录）：')\n",
    "    \n",
    "    if (dstpath == '') or os.path.exists(dstpath):\n",
    "        row = int(input('请输入切割行数：'))\n",
    "        col = int(input('请输入切割列数：'))\n",
    "        if row > 0 and col > 0:\n",
    "            splitimage(src, row, col, dstpath)\n",
    "        else:\n",
    "            print('无效的行列切割参数！')\n",
    "    else:\n",
    "        print('图片输出目录 %s 不存在！' % dstpath)\n",
    "else:\n",
    "    print('图片文件 %s 不存在！' % src)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "璇疯緭鍏ュ浘鐗囪緭鍑虹洰褰曪紙涓嶈緭鍏ヨ矾寰勫垯琛ㄧず浣跨敤婧愬浘鐗囨墍鍦ㄧ洰褰曪級锛�\"D:/PycharmProjects/sf_dataAnalysis/waterimg/1.png\"\n",
      "璇疯緭鍏ュ垏鍓插悗鍥剧墖琛岄珮鐨勪竴鍗婏細\"D:/PycharmProjects/sf_dataAnalysis/waterimg/\"\n"
     ]
    },
    {
     "ename": "ValueError",
     "evalue": "invalid literal for int() with base 10: 'D:/PycharmProjects/sf_dataAnalysis/waterimg/'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-13-d628eaa8abf3>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m     37\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     38\u001b[0m     \u001b[1;32mif\u001b[0m \u001b[1;33m(\u001b[0m\u001b[0mdstpath\u001b[0m \u001b[1;33m==\u001b[0m \u001b[1;34m''\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;32mor\u001b[0m \u001b[0mos\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mpath\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mexists\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mdstpath\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 39\u001b[1;33m         \u001b[0mhalfw\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0minput\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'请输入切割后图片行高的一半：'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     40\u001b[0m         \u001b[0mhalfh\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0minput\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'请输入切割后图片列宽的一半：'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     41\u001b[0m         \u001b[1;32mif\u001b[0m \u001b[0mrow\u001b[0m \u001b[1;33m>\u001b[0m \u001b[1;36m0\u001b[0m \u001b[1;32mand\u001b[0m \u001b[0mcol\u001b[0m \u001b[1;33m>\u001b[0m \u001b[1;36m0\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mValueError\u001b[0m: invalid literal for int() with base 10: 'D:/PycharmProjects/sf_dataAnalysis/waterimg/'"
     ]
    }
   ],
   "source": [
    "import os\n",
    "from PIL import Image\n",
    "\n",
    "def splitimage(src, halfw, halfh, dstpath):\n",
    "    img = Image.open(src)\n",
    "    w, h = img.size\n",
    "#     if rownum <= h and colnum <= w:\n",
    "#         print('Original image info: %sx%s, %s, %s' % (w, h, img.format, img.mode))\n",
    "#         print('开始处理图片切割, 请稍候...')\n",
    "\n",
    "    s = os.path.split(src)\n",
    "    if dstpath == '':\n",
    "        dstpath = s[0]\n",
    "    fn = s[1].split('.')\n",
    "    basename = fn[0]\n",
    "    ext = fn[-1]\n",
    "    \n",
    "    box = (w//2 -halfw, w//2 +halfw, h//2 -halfh, h//2 +halfh)\n",
    "    img.crop(box).save(os.path.join(dstpath, basename + '_' + str(num) + '.' + ext), ext)\n",
    "#         num = 0\n",
    "#         rowheight = h // rownum\n",
    "#         colwidth = w // colnum\n",
    "#         for r in range(rownum):\n",
    "#             for c in range(colnum):\n",
    "#                 box = (c * colwidth, r * rowheight, (c + 1) * colwidth, (r + 1) * rowheight)\n",
    "#                 img.crop(box).save(os.path.join(dstpath, basename + '_' + str(num) + '.' + ext), ext)\n",
    "#                 num = num + 1\n",
    "\n",
    "#         print('图片切割完毕，共生成 %s 张小图片。' % num)\n",
    "#     else:\n",
    "#         print('不合法的行列切割参数！')\n",
    "\n",
    "# src = input('请输入图片文件路径：')\n",
    "src = 'test.jpg'\n",
    "if os.path.isfile(src):\n",
    "    dstpath = input('请输入图片输出目录（不输入路径则表示使用源图片所在目录）：')\n",
    "    \n",
    "    if (dstpath == '') or os.path.exists(dstpath):\n",
    "        halfrow = int(input('请输入切割后图片行高的一半：'))\n",
    "        halfh = int(input('请输入切割后图片列宽的一半：'))\n",
    "        if row > 0 and col > 0:\n",
    "            splitimage(src, row, col, dstpath)\n",
    "        else:\n",
    "            print('无效的行列切割参数！')\n",
    "    else:\n",
    "        print('图片输出目录 %s 不存在！' % dstpath)\n",
    "else:\n",
    "    print('图片文件 %s 不存在！' % src)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "filename= r\"C:\\Users\\zhuwenjing\\Desktop\\test_cut.jpg\"\n",
    "img = cv2.imread(filename)\n",
    "img"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
